/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



import com.toto.entity.UserLocal;
import com.toto.entity.UserLocalHome;
import com.toto.entity.remote.User;
import com.toto.entity.remote.UserHome;
import java.rmi.RemoteException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

/**
 *
 * @author Administrator
 */
public class EntityBean2Client {
  public static void main(String[] args) throws NamingException
  {
    try
    {
      Context ctx = getInitialContext();
      Object obj = ctx.lookup("ejb/user");
      UserLocalHome userLocalHome = (UserLocalHome) PortableRemoteObject.narrow(obj, UserHome.class);
      userLocalHome.create("007", "ouliu", true);
      userLocalHome.create("0077", "ouliu", true);
      userLocalHome.create("008", "tao", false);
      Collection users = userLocalHome.findByName("ouliu");
      Iterator it = users.iterator();
      while (it.hasNext())
      {
        UserLocal user = (UserLocal) it.next();
        System.out.println(user.getUserId());
      }
    }
    catch (FinderException ex)
    {
      Logger.getLogger(EntityBean2Client.class.getName()).log(Level.SEVERE, null, ex);
    }    catch (CreateException ex)
    {
      Logger.getLogger(EntityBean2Client.class.getName()).log(Level.SEVERE, null, ex);
    }
  }
  public static InitialContext getInitialContext() throws NamingException
  {
    Properties p = new Properties();
    p.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
    p.setProperty(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
    p.setProperty(Context.PROVIDER_URL, "jnp://localhost:1099");
    return new InitialContext(p);
  }
}
